package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.back.SysUserBack;
import com.ruoyi.information.mapper.back.SysUserBackMapper;
import com.ruoyi.information.service.back.ISysUserBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-08
 */
@Service
public class SysUserBackServiceImpl implements ISysUserBackService {
    @Autowired
    private SysUserBackMapper sysUserMapper;


    /**
     * 查询用户列表 包含供给方和需求方
     *
     * @param sysUser 用户
     * @return 用户集合
     */
    @Override
    public List<SysUserBack> selectSysUserListAll(SysUserBack sysUser) {
        return sysUserMapper.selectSysUserListAll(sysUser);
    }

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public SysUserBack selectSysUserByUserId(Long userId) {
        return sysUserMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询用户列表
     *
     * @param sysUser 用户
     * @return 用户
     */
    @Override
    public List<SysUserBack> selectSysUserList(SysUserBack sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户
     *
     * @param sysUser 用户
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUserBack sysUser) {
        /*校验手机号*/
        if (sysUserMapper.checkPhoneNumber(sysUser) == 0) {
            sysUser.setCreateTime(DateUtils.getNowDate());
            sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
            sysUser.setUserName(sysUser.getPhonenumber());
            int i = sysUserMapper.insertSysUser(sysUser);
            switch (sysUser.getUserType()) {
                case "01":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 100);
                    break;
                case "02":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 101);
                    break;
                case "03":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 102);
                    break;
            }
            return i;
        } else {
            throw new RuntimeException("手机号已存在");
        }
    }

    /**
     * 修改用户
     *
     * @param sysUser 用户
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUserBack sysUser) {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        SysUserBack user = sysUserMapper.selectSysUserByUserId(sysUser.getUserId());
        /*检查手机号是否修改*/
        if (sysUser.getPhonenumber() != null && user.getPhonenumber().equals(sysUser.getPhonenumber())) {
//            return sysUserMapper.updateSysUser(sysUser);
        } else
            /*校验手机号*/
            if (sysUser.getPhonenumber() != null) {
                if (sysUserMapper.checkPhoneNumber(sysUser) == 0) {
                    sysUser.setUserName(sysUser.getPhonenumber());
                } else {
                    throw new RuntimeException("手机号已存在");
                }
            }
        /*更改角色*/
        if (sysUser.getUserType() != null && !sysUser.getUserType().equals("") && user.getUserType() != null && !user.getUserType().equals(sysUser.getUserType())) {
            /*删除原有角色权限*/
            sysUserMapper.deleteSysUserRoleByUserId(sysUser.getUserId());
            switch (sysUser.getUserType()) {
                case "01":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 100);
                    break;
                case "02":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 101);
                    break;
                case "03":
                    sysUserMapper.insertUserRole(sysUser.getUserId(), 102);
                    break;
            }
        }

        return sysUserMapper.updateSysUser(sysUser);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserIds(Long[] userIds) {
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserId(Long userId) {
        return sysUserMapper.deleteSysUserByUserId(userId);
    }
}
